package chapter8.section6;

//程序 8-9 利用排序集合保存实现了排序接口的 Account 对象

import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;

public class SortedAccountRoster {
    private final Set<Account> roster = new TreeSet<>();

    public static void main(String[] args) {
        SortedAccountRoster r = new SortedAccountRoster();
        r.add(new Account("001", "张三"));
        r.add(new Account("003", "李四"));
        r.add(new Account("002", "王五"));
        System.out.println("--列出当前所有的账户，注意输出顺序和添加时的顺序--");
        r.print();
    }

    // 获取花名册中账户的数量
    public int size() {
        return roster.size();
    }

    public void add(Account account) {
        roster.add(account);
    }

    // 显示所有账户信息到默认输出
    public void print() {
        Iterator<Account> it = roster.iterator();
        while (it.hasNext()) {
            Account account = it.next();
            System.out.println(account);
        }
    }

    // 寻找集合中是否存在指定id的账户
    public Account find(String id) {
        Account result = null;
        for (Account account : roster) {
            if (account.getId().equals(id)) {// ����Ŀ��
                result = account;
                break;
            }
        }
        return result;
    }
}
